package com.cn.api.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test3 {
    public static int num = 0;
    public static BigDecimal[] big1 = new BigDecimal[200];
    public static BigDecimal[][] big2 = new BigDecimal[10][10];

    public static void each(Callback callback, int times) {
        //价格列表
        BigDecimal[] indexes = {new BigDecimal("20"), new BigDecimal("48"), new BigDecimal("29.9")};
//满减价格
        BigDecimal fullReduction = new BigDecimal("90");
        BigDecimal i4 = new BigDecimal("25");
        System.out.println("按单个物品计算出每个物品最多可购买件数");
        List<BigDecimal> maxBuy = new ArrayList<>();
        Arrays.asList(indexes).stream().sorted((x, y) -> y.compareTo(x)).forEach((x) -> {
            Integer max = (fullReduction.intValue() % x.intValue() == 0 ? fullReduction.intValue() / x.intValue() : fullReduction.intValue() / x.intValue() + 1);
            //满减的最高价格，做范围用
//            BigDecimal[] b = {BigDecimal.valueOf(max), x.multiply(BigDecimal.valueOf(max))};
            maxBuy.add(BigDecimal.valueOf(max));
            System.out.println("价格为  " + x + "  可以买  " + max + "  件,总价为" + x.multiply(BigDecimal.valueOf(max)));
        });
//        each0(callback, times, 0, indexes, maxBuy);
        each0(callback, maxBuy.size(), 0, indexes, maxBuy);
//        each0(callback, maxBuy.get(0).intValue(), 0, new BigDecimal[indexes.length], maxBuy);
    }

    /**
     * @param callback
     * @param times  循环次数
     * @param nextTimes 下次循环次数
     * @param indexes  数租
     * @param maxBuy list
     */
    private static void each0(Callback callback, int times, int nextTimes,
                              BigDecimal[] indexes, List<BigDecimal> maxBuy) {
        System.out.println(times+"11111111111111111111111");
        if (nextTimes > times-1)
            return;
        if (times > maxBuy.size()){
            return;
        }
        for (int i = 0; i < times-1; i++) {
            //indexes[nextTimes] = BigDecimal.valueOf(i);
            int[] ints = new int[indexes.length];
//            for (int k = indexes.length; --k > nextTimes; ) {
//                indexes[k] = -1;
//            }
//如果这次的嵌套循环次数没达到，不执行以下代码
//            if (nextTimes == indexes.length - 1)
                callback.call(indexes, nextTimes);

            //迭代 ，对times更改，因为每次循环的次数可能不一致
            //当嘴里层循环完，换
//            num++;
        }

        for (int j=0 ;j<=maxBuy.get(nextTimes).intValue();j++){
           // big1[num] = BigDecimal.valueOf(j);
            big2[nextTimes][j] = BigDecimal.valueOf(j);
            callback.call(indexes, nextTimes);
        }
        each0(callback, times, nextTimes + 1, indexes, maxBuy);
    }

    public static interface Callback {
        public void call(BigDecimal[] indexes, int deep);
    }

    public static void main(String[] args) {
        Test3.each(new Callback() {
            int i = 1;

            public void call(BigDecimal[] indexes, int deep) {
                System.out.println(Arrays.toString(indexes) + ", 当前嵌套层数:" + deep + "执行了" + (i++) + "次");
            }
        }, 3);
        System.out.println( Arrays.asList(big2));
    }




}





